package com.slipper.system.subsystem.controller;

import com.google.gson.reflect.TypeToken;
import com.slipper.constants.Constants;
import com.slipper.core.gson.GsonUtil;
import com.slipper.util.*;
import jakarta.annotation.Resource;

import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.subsystem.bo.SubsystemBO;
import com.slipper.system.subsystem.service.SubsystemService;

import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

/**
 * 子系统信息表
 */
@RestController
@RequestMapping("/system/subsystem")
public class SubsystemController {
    private static final Logger logger = LoggerFactory.getLogger(SubsystemController.class);

    @Resource
    private SubsystemService subsystemService;

    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody SubsystemBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(subsystemService.queryBy(bo.getId()));
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody SubsystemBO bo) {
        return ResultUtil.successResult(subsystemService.queryList(bo));
    }

    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody SubsystemBO bo) {
        return ResultUtil.successResult(subsystemService.queryPage(bo));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody SubsystemBO bo) {
        bo.setOperateId(Constants.getCurrentUserId());
        return ResultUtil.saveResult(subsystemService.save(bo));
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody SubsystemBO bo) {
        return ResultUtil.deleteResult(subsystemService.delete(bo));
    }

    @PostMapping(value = "/exportData")
    public void exportData(HttpServletResponse response, @RequestBody SubsystemBO bo) throws IOException {
        if (bo.getIds() == null || bo.getIds().length == 0) {
            ResultUtil.failureResult(response, 400, "参数不正确！");
            return;
        }

        List<SubsystemBO> subsystemList = subsystemService.queryList(bo.getIds());
        String fileName = "subsystem-" + DateUtil.format(new Date(), "dd-HH-mm-ss") + ".dat";
        FileUtil.downloadContent(response, fileName, AESUtil.encrypt(GsonUtil.toJson(subsystemList)));
    }

    @PostMapping(value = "/importData")
    public String importData(SubsystemBO bo) {
        if (bo.getFile() == null) {
            return ResultUtil.failureResult("请选择上传文件！");
        }

        try {
            String content = new String(bo.getFile().getBytes(), StandardCharsets.UTF_8);
            Type type = new TypeToken<List<SubsystemBO>>(){}.getType();
            List<SubsystemBO> subsystemList = GsonUtil.fromJson(AESUtil.decrypt(content), type);
            subsystemService.saveList(subsystemList);
            return ResultUtil.successResult("导入子系统成功。");
        } catch (Exception e) {
            logger.error("文件解释出错！", e);
            return ResultUtil.failureResult("文件解释出错！");
        }
    }
}
